package com.shen.shennetwork

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.wifi.WifiManager
import android.os.Build

/**
 * author:  shen
 * date:    2021/11/7
 *
 */
object NetworkUtils {

    fun initWifiManager(context: Context) {
        if(isWifiConnected(context)){
            val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
            wifiManager.connectionInfo.toString()
        }
    }

    fun getCurrWifiInfo(context: Context?): String{
        if (context != null) {
            val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                val wifiNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                if (wifiNetworkInfo != null) {
                    return  "${wifiNetworkInfo.toString()}\n***\n${wifiManager.connectionInfo.toString()}"
                }
            } else {
                val network = manager.activeNetwork ?: return "未链接Wifi网络"
                val status = manager.getNetworkCapabilities(network) ?: return "未链接Wifi网络"
                if (status.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    return "${network.toString()}\n***\n${status.toString()}\n***\n${wifiManager.connectionInfo.toString()}"
                }
            }
        }
        return "未链接Wifi网络"
    }


    /**
     * 判断网络是否连接
     */
    fun isNetworkConnected(context: Context?): Boolean {
        if (context != null) {
            val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                val networkInfo = manager.activeNetworkInfo
                if (networkInfo != null) {
                    return networkInfo.isAvailable or networkInfo.isConnected
                }
            } else {
                val network = manager.activeNetwork ?: return false
                val status = manager.getNetworkCapabilities(network) ?: return false
                if (status.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
                    return true
                }
            }
        }
        return false
    }

    /**
     * 判断是否是WiFi连接
     */
    fun isWifiConnected(context: Context?): Boolean {
        if (context != null) {
            val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                val wifiNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                if (wifiNetworkInfo != null) {
                    return wifiNetworkInfo.isAvailable
                }
            } else {
                val network = manager.activeNetwork ?: return false
                val status = manager.getNetworkCapabilities(network) ?: return false
                if (status.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    return true
                }
            }
        }
        return false
    }

    /**
     * 判断是否是数据网络连接
     */
    fun isMobileConnected(context: Context?): Boolean {
        if (context != null) {
            val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                val mobileNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                if (mobileNetworkInfo != null) {
                    return mobileNetworkInfo.isAvailable
                }
            } else {
                val network = manager.activeNetwork ?: return false
                val status = manager.getNetworkCapabilities(network) ?: return false
                // status.transportInfo
                if (status.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                    return true
                }
            }
        }
        return false
    }
}